import {
  resultSuccess,
  resultError,
  getRequestToken,
  requestParams,
  baseUrl
} from '../_util';
import { MockMethod } from 'vite-plugin-mock';
import { createFakeUserList } from './user';

const dashboardRoute = {
  path: '/dashboard',
  name: 'Dashboard',
  component: 'LAYOUT',
  redirect: '/dashboard/analysis',
  meta: {
    title: '仪表板',
    hideChildrenInMenu: true,
    icon: 'bx:bx-home'
  },
  children: [
    {
      path: 'analysis',
      name: 'Analysis',
      component: '/dashboard/analysis/index',
      meta: {
        hideMenu: true,
        hideBreadcrumb: true,
        title: '分析',
        currentActiveMenu: '/dashboard',
        icon: 'bx:bx-home'
      }
    },
    {
      path: 'workbench',
      name: 'Workbench',
      component: '/dashboard/workbench/index',
      meta: {
        hideMenu: true,
        hideBreadcrumb: true,
        title: '工作台',
        currentActiveMenu: '/dashboard',
        icon: 'bx:bx-home'
      }
    }
  ]
};

const compRoute = {
  path: '/comp2',
  name: 'comp2',
  component: 'LAYOUT',
  redirect: '/comp/button',
  meta: {
    icon: 'ion:settings-outline',
    title: '组件样例'
  },
  children: [
    {
      path: 'button',
      name: 'comp-button',
      meta: {
        icon: 'ion:settings-outline',
        title: '按钮'
      },
      component: '/demo/comp/button/index'
    },
    {
      path: 'card-list',
      name: 'comp-card-list',
      meta: {
        icon: 'ion:settings-outline',
        title: '卡片'
      },
      component: '/demo/comp/card-list/index'
    },
    {
      path: 'count-to',
      name: 'comp-count-to',
      meta: {
        icon: 'ion:settings-outline',
        title: '数字'
      },
      component: '/demo/comp/count-to/index'
    },
    {
      path: 'cropper',
      name: 'comp-cropper',
      meta: {
        icon: 'ion:settings-outline',
        title: '图片裁剪'
      },
      component: '/demo/comp/cropper/index'
    },
    {
      path: 'desc',
      name: 'comp-desc',
      meta: {
        icon: 'ion:settings-outline',
        title: '详情'
      },
      component: '/demo/comp/desc/index'
    },
    {
      path: 'drawer',
      name: 'comp-drawer',
      meta: {
        icon: 'ion:settings-outline',
        title: '抽屉'
      },
      component: '/demo/comp/drawer/index'
    },
    {
      path: 'lazy',
      name: 'comp-lazy',
      meta: {
        icon: 'ion:settings-outline',
        title: '懒加载'
      },
      redirect: '/comp/lazy-base',
      children: [
        {
          path: 'lazy-base',
          name: 'comp-lazy-base',
          meta: {
            icon: 'ion:settings-outline',
            title: '懒加载'
          },
          component: '/demo/comp/lazy/index'
        },
        {
          path: 'lazy-animation',
          name: 'comp-lazy-animation',
          meta: {
            icon: 'ion:settings-outline',
            title: '懒加载-动画'
          },
          component: '/demo/comp/lazy/Transition'
        }
      ]
    },
    {
      path: 'loading',
      name: 'comp-loading',
      meta: {
        icon: 'ion:settings-outline',
        title: '加载'
      },
      component: '/demo/comp/loading/index'
    },
    {
      path: 'modal',
      name: 'comp-modal',
      meta: {
        icon: 'ion:settings-outline',
        title: '模态窗'
      },
      component: '/demo/comp/modal/index'
    },
    {
      path: 'qrcode',
      name: 'comp-qrcode',
      meta: {
        icon: 'ion:settings-outline',
        title: '二维码'
      },
      component: '/demo/comp/qrcode/index'
    },
    {
      path: 'scroll',
      name: 'comp-scroll',
      meta: {
        icon: 'ion:settings-outline',
        title: '二维码'
      },
      component: '/demo/comp/scroll/index'
    }
  ]
};

const authRoute = {
  path: '/permission',
  name: 'Permission',
  component: 'LAYOUT',
  redirect: '/permission/front/page',
  meta: {
    icon: 'carbon:user-role',
    title: '权限'
  },
  children: [
    {
      path: 'back',
      name: 'PermissionBackDemo',
      meta: {
        title: '权限'
      },

      children: [
        {
          path: 'page',
          name: 'BackAuthPage',
          component: '/demo/permission/back/index',
          meta: {
            title: '权限'
          }
        },
        {
          path: 'btn',
          name: 'BackAuthBtn',
          component: '/demo/permission/back/Btn',
          meta: {
            title: '权限切换按钮'
          }
        }
      ]
    }
  ]
};

const levelRoute = {
  path: '/level',
  name: 'Level',
  component: 'LAYOUT',
  redirect: '/level/menu1/menu1-1',
  meta: {
    icon: 'carbon:user-role',
    title: '多级菜单'
  },
  children: [
    {
      path: 'menu1',
      name: 'Menu1Demo',
      meta: {
        title: '菜单1'
      },
      children: [
        {
          path: 'menu1-1',
          name: 'Menu11Demo',
          meta: {
            title: '菜单1-1'
          },
          children: [
            {
              path: 'menu1-1-1',
              name: 'Menu111Demo',
              component: '/demo/level/Menu111',
              meta: {
                title: '菜单1-1-1'
              }
            }
          ]
        },
        {
          path: 'menu1-2',
          name: 'Menu12Demo',
          component: '/demo/level/Menu12',
          meta: {
            title: '菜单1-2'
          }
        }
      ]
    },
    {
      path: 'menu2',
      name: 'Menu2Demo',
      component: '/demo/level/Menu2',
      meta: {
        title: '菜单2'
      }
    }
  ]
};

const systemRoute = {
  path: '/system',
  name: 'System',
  component: 'LAYOUT',
  redirect: '/system/account',
  meta: {
    icon: 'ion:settings-outline',
    title: '模块'
  },
  children: [
    {
      path: 'account',
      name: 'AccountManagement',
      meta: {
        title: '账号',
        ignoreKeepAlive: true
      },
      component: '/demo/system/account/index'
    },
    {
      path: 'account_detail/:id',
      name: 'AccountDetail',
      meta: {
        hideMenu: true,
        title: '账号详情',
        ignoreKeepAlive: true,
        showMenu: false,
        currentActiveMenu: '/system/account'
      },
      component: '/demo/system/account/AccountDetail'
    },
    {
      path: 'role',
      name: 'RoleManagement',
      meta: {
        title: '角色',
        ignoreKeepAlive: true
      },
      component: '/demo/system/role/index'
    },

    {
      path: 'menu',
      name: 'MenuManagement',
      meta: {
        title: '菜单',
        ignoreKeepAlive: true
      },
      component: '/demo/system/menu/index'
    },
    {
      path: 'dept',
      name: 'DeptManagement',
      meta: {
        title: '部门',
        ignoreKeepAlive: true
      },
      component: '/demo/system/dept/index'
    },
    {
      path: 'changePassword',
      name: 'ChangePassword',
      meta: {
        title: '更改密码',
        ignoreKeepAlive: true
      },
      component: '/demo/system/password/index'
    },
    {
      path: 'test',
      name: 'test',
      meta: {
        title: '测试',
        ignoreKeepAlive: true
      },
      component: '/demo/system/test/index'
    }
  ]
};

const linkRoute = {
  path: '/link',
  name: 'Link',
  component: 'LAYOUT',
  meta: {
    icon: 'ion:tv-outline',
    title: '框架'
  },
  children: [
    {
      path: 'doc',
      name: 'Doc',
      meta: {
        title: '文档',
        frameSrc: 'https://vvbin.cn/doc-next/'
      }
    },
    {
      path: 'https://vvbin.cn/doc-next/',
      name: 'DocExternal',
      component: 'LAYOUT',
      meta: {
        title: '扩展文档'
      }
    }
  ]
};

export default [
  {
    url: `${baseUrl}/sys/permission/getUserPermissionByToken`,
    timeout: 1000,
    method: 'get',
    response: (request: requestParams) => {
      const token = getRequestToken(request);
      if (!token) {
        return resultError('Invalid token!');
      }
      const checkUser = createFakeUserList().find(
        (item) => item.token === token
      );
      if (!checkUser) {
        return resultError('Invalid user token!');
      }
      const id = checkUser.userId;
      let menu: Object[];
      switch (id) {
        case '1':
          dashboardRoute.redirect =
            dashboardRoute.path + '/' + dashboardRoute.children[0].path;
          menu = [
            dashboardRoute,
            compRoute,
            authRoute,
            levelRoute,
            systemRoute,
            linkRoute
          ];
          break;
        case '2':
          dashboardRoute.redirect =
            dashboardRoute.path + '/' + dashboardRoute.children[1].path;
          menu = [dashboardRoute, authRoute, levelRoute, linkRoute];
          break;
        default:
          menu = [];
      }

      return resultSuccess(menu);
    }
  },
  {
    url: `${baseUrl}/system/getMenuList`,
    method: 'get',
    response() {
      const menu = [
        dashboardRoute,
        authRoute,
        levelRoute,
        systemRoute,
        linkRoute,
        compRoute
      ];
      return resultSuccess(menu);
    }
  }
] as MockMethod[];
